package com.itheima.d6_tcp3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketMultiTask implements Runnable {
    private Socket socket;

    public SocketMultiTask() {

    }

    // 创建对象时拿到请求连接对象
    public SocketMultiTask(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        // try () 内实现了AutoCloseable的对象可以自动close释放资源
        try (Socket ss = this.socket;){
            // 3、从socket通信管道中得到一个字节输入流。
            final InputStream is = ss.getInputStream();

            // 4、把原始的字节输入流包装成高效的输入流
            final BufferedReader br = new BufferedReader(new InputStreamReader(is));

            // 5、使用数据输入流读取客户端发送过来的消息
            final String result = br.readLine();
            System.out.println(result);

            // 6. 查看ip
            System.out.println("来自IP：" + ss.getInetAddress() + " port：" + ss.getPort());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
